package com.gaoyi.crm.fragment.ctm;

import androidx.activity.result.ActivityResult;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.gaoyi.crm.R;
import com.gaoyi.crm.adapter.SyncedCusAdapter;
import com.gaoyi.crm.callback.IView;
import com.gaoyi.crm.control.dialog.LoadingDialogUtils;
import com.gaoyi.crm.databinding.FragmentCtmSyncedCusBinding;
import com.gaoyi.crm.db.dto.SyncedCustomerDto;
import com.gaoyi.crm.db.query.CtmSyncedQuery;
import com.gaoyi.crm.fragment.base.BaseChildListFragment;
import com.gaoyi.crm.util.Utils;
import com.gaoyi.crm.vm.CtmSyncedCusViewModel;

import java.util.Date;
import java.util.Map;

public class CtmSyncedCusFragment extends BaseChildListFragment<FragmentCtmSyncedCusBinding, CtmSyncedCusViewModel, SyncedCustomerDto, CtmSyncedQuery> implements IView {

    private SyncedCusAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_ctm_synced_cus;
    }

    @Override
    protected void initView() {
        mDateSearchView = mBinding.mobileInclude.viewDateSearch;
        mViewSearch = mBinding.mobileInclude.viewSearch;
        mSmartRefreshLayout = mBinding.smartRefreshLayout;
        serTitleSort(mBinding.titleSyncedAt, mBinding.titleStatus);
        mAdapter = new SyncedCusAdapter(mContext);
        mBinding.rvSyncedCus.setLayoutManager(new LinearLayoutManager(mContext));
        mBinding.rvSyncedCus.setAdapter(mAdapter);
        mBinding.rvSyncedCus.setItemAnimator(null);
    }

    @Override
    protected void initData() {
        showLoading();
        mQuery.init();
        mViewModel.getSyncedCustomer(mQuery);
    }

    @Override
    protected void initLiveData() {
        mViewModel.getTotal().observe(mParent, integer -> mBinding.keyValueCtm.setValue(new String[]{getString(R.string.total_item, integer)}));
        mViewModel.getResultCode().observe(mParent, integer -> Utils.refreshSmartRefreshLayout(mBinding.smartRefreshLayout));
        mViewModel.getList().observe(mParent, syncedCustomerDtos -> mAdapter.submitList(syncedCustomerDtos));
    }

    @Override
    protected void loadData() {
        mViewModel.getSyncedCustomer(mQuery);
    }

    @Override
    protected void search(Date startDate, Date endDate) {
        mViewModel.clearListValue();
        mQuery.setEndDate(endDate.getTime());
        mQuery.setStartDate(startDate.getTime());
        mQuery.setKeyWord(mBinding.mobileInclude.viewSearch.getQuery().toString());
        mQuery.init();
        mViewModel.getSyncedCustomer(mQuery);
    }

    @Override
    protected void search(String keyWord) {
        mViewModel.clearListValue();
        if (mDateSearchView.isSelect()) {
            mQuery.setStartDate(mDateSearchView.getStartDate().getTime());
            mQuery.setEndDate(mDateSearchView.getEndDate().getTime());
        }
        mQuery.setKeyWord(keyWord);
        mQuery.init();
        mViewModel.getSyncedCustomer(mQuery);
    }

    @Override
    protected void sort(int id, Integer sort) {
        if (id == R.id.title_status) {
            mQuery.setOrderBySyncStatus(sort);
        } else if (id == R.id.title_synced_at) {
            mQuery.setOrderBySyncedAt(sort);
        }
        mViewModel.clearListValue();
        mQuery.init();
        mViewModel.getSyncedCustomer(mQuery);
    }

    @Override
    protected CtmSyncedQuery createQuery() {
        return new CtmSyncedQuery();
    }

    @Override
    protected void onRequestPermissionsResult(Map<String, Boolean> result) {

    }

    @Override
    protected void onActivityResult(int requestCode, ActivityResult result) {

    }

    @Override
    public void showLoading() {
        LoadingDialogUtils.getInstance().showLoading(mContext);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtils.getInstance().dismiss();
    }

}